package com.tronsis.distribution.activity.training;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageAndVideoPreviewSeeActivity;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.CourseDetailBean;
import com.tronsis.distribution.biz.factory.TeamBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.imageloader.GlideApp;
import com.tronsis.library.util.DateUtil;
import com.tronsis.library.util.DisplayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;

public class CourseDetailActivity extends ToolBarBaseActivity {

    @BindView(R.id.banner_guide_content)
    BGABanner mBanner;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_creator)
    TextView mTvCreator;
    @BindView(R.id.tv_createdOn)
    TextView mTvCreatedOn;
    @BindView(R.id.video_player)
    StandardGSYVideoPlayer mVideoPlayer;
    @BindView(R.id.tv_description)
    TextView mTvDescription;

    CourseDetailBean mCourseDetailBean;
    private List<String> mBannerBeanList = new ArrayList<>();
    private ArrayList<ImageItem> mImageItems = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.course_detail));

        initBanner();

        Intent intent = getIntent();
        String courseId = intent.getStringExtra("courseId");
        httpCourseDetail(courseId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoPlayer.onVideoPause();
    }

    private void initBanner() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mBanner.getLayoutParams();
        params.height = DisplayUtil.getInstance(mContext).getScreenWidth() * 8 / 15;
        mBanner.setLayoutParams(params);
        mBanner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String bean, final int position) {
                GlideApp.with(mContext).load(Url.FILE_DOWNLOAD + bean).placeholder(R.drawable.home_banner).error(R.drawable
                        .home_banner).centerCrop().into(itemView);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intentPreview = new Intent(CourseDetailActivity.this, ImageAndVideoPreviewSeeActivity.class);
                        intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                        intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mImageItems);
                        startActivity(intentPreview);
                    }
                });

            }
        });

    }

    private void httpCourseDetail(String courseId) {
        try {
            TeamBizFactory.createBiz().getCourseDetail(this, courseId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    CourseDetailBean courseDetailBean = (CourseDetailBean) data;
                    setDetailView(courseDetailBean);
                    dismissLoadingDialog();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void setDetailView(CourseDetailBean detailBean) {
        if (detailBean == null) {
            return;
        }
        mCourseDetailBean = detailBean;
        mTvName.setText(detailBean.getName());
        mTvCreator.setText(getString(R.string.format_create_name,detailBean.getCreatorName()));
        mTvCreatedOn.setText(getString(R.string.format_time, DateUtil.getDateTimeString(detailBean.getCreatedOn())));
        mTvDescription.setText(detailBean.getDescription());

        String fid = detailBean.getImageFid();
        if (fid != null) {
            String[] split = fid.split(",");
            mBannerBeanList.clear();
            mBannerBeanList.addAll(Arrays.asList(split));
            mImageItems.clear();
            for (String string : mBannerBeanList) {
                ImageItem imageItem = new ImageItem();
                imageItem.url = Url.FILE_DOWNLOAD + string;
                mImageItems.add(imageItem);
            }
            mBanner.setData(mBannerBeanList, null);
        }

        String videoId = detailBean.getVideoFid();
        mVideoPlayer.getBackButton().setVisibility(View.GONE);
        mVideoPlayer.getFullscreenButton().setVisibility(View.VISIBLE);
        mVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mVideoPlayer.startWindowFullscreen(mContext, true, true);
            }
        });
        if (!TextUtils.isEmpty(videoId)) {
            mVideoPlayer.setVisibility(View.VISIBLE);
            mVideoPlayer.setUp(Url.FILE_DOWNLOAD+videoId, false, null);
        } else {
            mVideoPlayer.setVisibility(View.GONE);
        }
    }


}
